//package com.activemqwrap.tools.tcpforward;
//
//import org.apache.activemq.ActiveMQConnection;
//import org.apache.activemq.ActiveMQConnectionFactory;
//import org.apache.activemq.advisory.DestinationSource;
//import org.apache.activemq.command.ActiveMQQueue;
//
//import javax.jms.*;
//import java.io.OutputStream;
//import java.util.Base64;
//import java.util.Set;
//
//public class PushPopActivMQ {
//    private final String DEFAULT_BROKER_HOST = "tcp://192.168.1.230:61616";
//    private final String usarName = "system";
//    private final String password = "manager";
//    private ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
//            usarName,
//            password,
//            DEFAULT_BROKER_HOST
//    );
//    public int sleepTime = 10;
//
//    public void destroyAllQueue(String queueName) {
//        try {
//            ActiveMQConnection conn = (ActiveMQConnection) connectionFactory.createConnection();
//            conn.destroyDestination(new ActiveMQQueue(queueName));
//        } catch (Exception e) {
//            System.out.println("destroyAllQueue---" + e.getMessage());
//        }
//    }
//
//    public void getAllQueueAndDelete() {
//        int stopFlag = 0;
//
//        while (stopFlag < 5) {
//            try {
//                ActiveMQConnection connection = (ActiveMQConnection) connectionFactory.createConnection();
//                connection.start();
//                DestinationSource ds = connection.getDestinationSource();
//                Set<ActiveMQQueue> queues = ds.getQueues();
//
//                for (ActiveMQQueue activeMQQueue : queues) {
//                    try {
//                        String queueName = activeMQQueue.getQueueName();
//                        destroyAllQueue(queueName);
////                    System.out.println("Deleted---" + activeMQQueue.getQueueName());
//                    } catch (Exception e) {
//                        System.out.println("getAllQueueAndDelete" + e.getMessage());
//                    }
//                }
//                if (queues.size() == 0) {
//                    stopFlag++;
//                }
//                System.out.println("deleted--" + queues.size());
//                connection.close();
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }
//
//    }
//
//    public void sendMQ(String queueName, String msg) throws Exception {
//        Connection connection = connectionFactory.createConnection();
//        // 启动
//        connection.start();
//        // 创建会话 session，参数第一个是事务，第二个是签收
//        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        // 创建目的地，queue 或者 topic
//        Queue queue = session.createQueue(queueName);
//        // 创建消息的生产者
//        MessageProducer producer = session.createProducer(queue);
//        // 创建消息
//        TextMessage textMessage = session.createTextMessage(msg);
//        // 发送消息给 mq
//        producer.send(textMessage);
//
//        // 关闭
//        session.close();
//        connection.close();
////        System.out.println("队列消息发送成功~" + queueName);
//    }
//
//    public void rcvMQ(String queueName) throws Exception {
//        Connection connection = connectionFactory.createConnection();
//        connection.start();
//        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Queue queue = session.createQueue(queueName);
//        // 创建消费者
//        MessageConsumer consumer = session.createConsumer(queue);
//        while (true) {
//            // 同步阻塞方式使用 receive()，超时之前一直等待
//            // receive() 方法不带参数会一直等待
//            // receive(Long timeout) 会等待指定时间后退出等待
//            TextMessage receive = (TextMessage) consumer.receive();
//            if (receive != null) {
////                System.out.println("接收到消息：" + receive.getText());
//            } else {
//                break;
//            }
////            Thread.sleep(sleepTime);
//        }
//        consumer.close();
//        session.close();
//        connection.close();
//    }
//
//    //等待新socket
//    public void rcvMQ_waitingSocket(String queueName) throws Exception {
//        Connection connection = connectionFactory.createConnection();
//        connection.start();
//        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Queue queue = session.createQueue(queueName);
//        // 创建消费者
//        MessageConsumer consumer = session.createConsumer(queue);
//        while (true) {
//            // 同步阻塞方式使用 receive()，超时之前一直等待
//            // receive() 方法不带参数会一直等待
//            // receive(Long timeout) 会等待指定时间后退出等待
//            TextMessage receive = (TextMessage) consumer.receive();
//            if (receive != null) {
//                String waiting_msg = receive.getText();
//                new Thread(() -> {
//                    SoketAndMQ.trsThread(waiting_msg);
//                }).start();
////                System.out.println("接收到队列消息-rcvMQ_waitingSocket：" + waiting_msg);
//
//            } else {
//                break;
//            }
////            Thread.sleep(sleepTime);
//        }
//        consumer.close();
//        session.close();
//        connection.close();
//    }
//
//    //获取 队列消息 连接目标 soket 转发到目标
//    public void MQ_trsToTarget(String queueName, OutputStream os) throws Exception {
//        Connection connection = connectionFactory.createConnection();
//        connection.start();
//        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//        Queue queue = session.createQueue(queueName);
//        // 创建消费者
//        MessageConsumer consumer = session.createConsumer(queue);
//        while (true) {
//            // 同步阻塞方式使用 receive()，超时之前一直等待
//            // receive() 方法不带参数会一直等待
//            // receive(Long timeout) 会等待指定时间后退出等待
//            TextMessage receive = (TextMessage) consumer.receive();
//            if (receive != null) {
//                String msg = receive.getText();
//                byte[] nb = Base64.getDecoder().decode(msg);
//                os.write(nb, 0, nb.length);
//                System.out.println("队列数据-转socket：" + queueName);
////                System.out.println("接收到队列消息-rcvMQ_trsThread：" + msg);
//            } else {
//                break;
//            }
////            Thread.sleep(sleepTime);
//        }
//        consumer.close();
//        session.close();
//        connection.close();
//    }
//
////    //获取返回数据队列 转发到 输入socket
////    public  void rcvMQ_rcvThread(String queueName, OutputStream os) throws Exception {
////        Connection connection = connectionFactory.createConnection();
////        connection.start();
////        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
////        Queue queue = session.createQueue(queueName);
////        // 创建消费者
////        MessageConsumer consumer = session.createConsumer(queue);
////        while (true) {
////            // 同步阻塞方式使用 receive()，超时之前一直等待
////            // receive() 方法不带参数会一直等待
////            // receive(Long timeout) 会等待指定时间后退出等待
////            TextMessage receive = (TextMessage) consumer.receive();
////            if (receive != null) {
////                String msg = receive.getText();
////                byte[] nb = Base64.getDecoder().decode(msg);
////                os.write(nb, 0, nb.length);
////                System.out.println("接收到队列消息-rcvMQ_trsThread：" + msg);
////            } else {
////                break;
////            }
////        }
////        consumer.close();
////        session.close();
////        connection.close();
////    }
//
////    public  void borwserQueue() throws Exception {
////        //1.获取连接工厂
////        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
////                usarName,
////                password,
////                DEFAULT_BROKER_HOST
////        );
////        //2.获取一个向ActiveMQ的连接
////        Connection connection = connectionFactory.createConnection();
////        connection.start();
////        //3.获取session
////        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
////        //4.找目的地，获取destination，消费端也会从这个目的地取消息
////        Queue queue = session.createQueue("user");
////
////        //5.消息消费者
////        QueueBrowser browser = session.createBrowser(queue);
////        Enumeration enumeration = browser.getEnumeration();
////        while (enumeration.hasMoreElements()) {
////            TextMessage element = (TextMessage) enumeration.nextElement();
////            System.out.println("浏览消息：" + element.getText());
////        }
////
////        // 关闭
////        browser.close();
////        session.close();
////        connection.close();
////
////    }
//
//
//}
